// Visual_BuilderDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Visual_Builder.h"
#include "Visual_BuilderDlg.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVisual_BuilderDlg dialog

CVisual_BuilderDlg::CVisual_BuilderDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVisual_BuilderDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVisual_BuilderDlg)
	m_PathXls = _T("");
	m_Server = _T("");
	m_CheckZone = FALSE;
	m_CheckCell = FALSE;
	m_CheckObjt = FALSE;
	m_PathLog = _T("");
	m_CheckMacro = FALSE;
	m_CheckMicro = FALSE;
	m_CheckOseq = FALSE;
	m_CheckTool = FALSE;
	m_CheckPhas = FALSE;
	m_StartCond = FALSE;
	m_TcSeq = FALSE;
	m_ChkInfi = FALSE;
	m_ChkMntn = FALSE;
	m_TcErrCodes = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDC_MAIN);
}

void CVisual_BuilderDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVisual_BuilderDlg)
	DDX_Control(pDX, IDC_CMB_ZONES, m_cmbZones);
	DDX_Control(pDX, IDC_CHK_CH_B, m_chkChB);
	DDX_Control(pDX, IDC_CHK_CH_A, m_chkChA);
	DDX_Control(pDX, IDC_CHECK_KEEP, m_chkKeep);
	DDX_Control(pDX, IDC_OPEN_XLS, m_OpenXlsButton);
	DDX_Control(pDX, IDC_CONNECT, m_ConnectButton);
	DDX_Control(pDX, IDC_FILEVIEW, m_FileViewButton);
	DDX_Control(pDX, IDC_OPEN_LOG, m_OpenLog);
	DDX_Control(pDX, IDC_PROGRESS, m_ProgBar);
	DDX_Control(pDX, IDC_ACQUIRE, m_Acquire);
	DDX_Control(pDX, IDC_ECHO, m_ListEcho);
	DDX_Text(pDX, IDC_PATH_XLS, m_PathXls);
	DDX_Text(pDX, IDC_SEREVR, m_Server);
	DDX_Check(pDX, IDC_CHECK_ZONE, m_CheckZone);
	DDX_Check(pDX, IDC_CHECK_CELL, m_CheckCell);
	DDX_Check(pDX, IDC_CHECK_OBJT, m_CheckObjt);
	DDX_Text(pDX, IDC_PATH_LOG, m_PathLog);
	DDX_Check(pDX, IDC_CHECK_MACRO, m_CheckMacro);
	DDX_Check(pDX, IDC_CHECK_MICRO, m_CheckMicro);
	DDX_Check(pDX, IDC_CHECK_OSEQ, m_CheckOseq);
	DDX_Check(pDX, IDC_CHECK_PHAS, m_CheckPhas);
	DDX_Check(pDX, IDC_STARTCOND, m_StartCond);
	DDX_Check(pDX, IDC_TCSEQ, m_TcSeq);
	DDX_Check(pDX, IDC_CHK_INFI, m_ChkInfi);
	DDX_Check(pDX, IDC_CHK_MNTN, m_ChkMntn);
	DDX_Check(pDX, IDC_TC_ERR, m_TcErrCodes);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVisual_BuilderDlg, CDialog)
	//{{AFX_MSG_MAP(CVisual_BuilderDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN_XLS, OnOpenXls)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_ACQUIRE, OnAcquire)
	ON_BN_CLICKED(IDC_OPEN_LOG, OnOpenLog)
	ON_BN_CLICKED(IDC_FILEVIEW, OnFileview)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
	ON_MESSAGE( WM_TNT_BUILD, OnTntBuild )
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVisual_BuilderDlg message handlers

BOOL CVisual_BuilderDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	ApmsActive = false;
	XlsActive = false;
	m_PathXls = XLS_DEFAULT_PATH;
	m_PathLog = LOG_DEFAULT_PATH;
	m_Server = TNT_DEFAULT_SERVER;
	UpdateData(false);
	m_ProgBar.SetRange32(0, EXCEL_MAX_ROW_CELL );
	m_ProgBar.SetPos(0);
	m_ProgBar.SetStep(1);
	
	this->m_cmbZones.ResetContent();

	this->m_cmbZones.InsertString(0, _T("ALL ZONES"));
	this->m_cmbZones.SetItemData(0, 0);
	
	this->m_cmbZones.SetCurSel(0);

	this->m_cmbZones.EnableWindow(false);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVisual_BuilderDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVisual_BuilderDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVisual_BuilderDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/**********************************************************************************/
void CVisual_BuilderDlg::echo(BOOL type){
	SYSTEMTIME	timeStamp;
	TCHAR		timeStr[100];
	//char		*temp;
	TCHAR		temp[223];
	
	
	if(_tcscmp(msg, _T("sp")) != 0){
		GetSystemTime(&timeStamp);
		_stprintf_s(timeStr, _T("[hh%02d  mm%02d  ss%02d,%03d  (GMT)]"), timeStamp.wHour, timeStamp.wMinute, timeStamp.wSecond, timeStamp.wMilliseconds);
		//temp = new char[sizeof(timeStamp) + sizeof(msg)];
		_stprintf_s(temp, _T("%s\t%s"), timeStr, msg);
		m_ListEcho.InsertString(m_ListEcho.GetCount(), temp);
		Sleep(100);
		UpdateData(true);
	}
	else{
		//temp = new char[223];
		_stprintf_s(temp, _T("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"));
		m_ListEcho.InsertString(m_ListEcho.GetCount(), temp);
		Sleep(100);
		UpdateData(true);
	}
}

/**********************************************************************************/
void CVisual_BuilderDlg::OnOpenXls() 
{
	

	CFileDialog	fileXls(TRUE, _T("xls"),_T(""), NULL,_T("Excel worksheet files|*.xls|All files|*.*||"));
	if(fileXls.DoModal() == IDOK){
		m_PathXls.Empty();
		m_PathXls = fileXls.GetPathName();
		UpdateData(false);
	}	
}

/**********************************************************************************/
void CVisual_BuilderDlg::OnConnect() 
{
	int			res_i;
	BOOL		res_b;
	TCHAR		*temp;
	
	UpdateData(true);
	if(!ApmsActive){
		temp = new TCHAR[sizeof(m_Server)];
		//_stprintf_s(temp, m_Server);
		
		_tcscpy_s(temp,sizeof(temp), m_Server);
		if(!_tcscmp(m_Server, _T("")))
			m_Server.Format(_T("Local"));
		res_i = ActivateApmsServer(temp, &m_clientNum, true);
		if(res_i){
			_stprintf_s(msg, _T("Connection to \"%s\" Apms server failed."), m_Server);
			echo(true);
		}
		else{
			_stprintf_s(msg, _T("Connected to \"%s\" Apms server."), m_Server);
			echo(true);
			ApmsActive = true;
		}
		fillZones();
	}
	else{
		_stprintf_s(msg, _T("Apms server already connected."));
		echo(true);
	}

	if(!CheckUser())
		return;

	if(!XlsActive){
		excelObj = new CXlsObj;
		excelObj->Init();
		
		res_b = excelObj->OpenWorkBook(m_PathXls, VARIANT_FALSE, 1);
		if(res_b == false){
			XlsActive = false;
			_stprintf_s(msg, _T("Excel file open failed."));
			echo(true);
		}
		else{
			XlsActive = true;
			_stprintf_s(msg, _T("Excel file open completed."));
			echo(true);
		}
	}
	else{
		XlsActive = true;
		_stprintf_s(msg, _T("Xls document %s already opened."), m_PathXls);
		echo(true);
	}
	
	_stprintf_s(msg, _T("sp"));
	echo(true);
}

/**********************************************************************************/
void CVisual_BuilderDlg::OnExit() 
{
	int		res_i;
	BOOL	res_b;
	
	_stprintf_s(msg, _T("Exiting..."));
	echo(true);
	
	if(ApmsActive){
		res_i = DeactivateApmsServer();
		if(res_i){
			_stprintf_s(msg, _T("Disonnection to %s Apms server failed."), m_Server);
			echo(true);
		}
		else{
			_stprintf_s(msg, _T("Apms server %s disconnected"), m_Server);
			echo(true);
			ApmsActive = false;
		}
	}

	if(XlsActive){
		res_b = excelObj->Close();
		if(res_b == false){
			_stprintf_s(msg, _T("Error closing Excel document."));
			echo(true);
		}
		else{
			_stprintf_s(msg, _T("Excel document closed."));
			echo(true);
			XlsActive = false;
		}
	}
	
	if(XlsActive || ApmsActive){
		res_i = MessageBox(_T("Either an Excel file is still opened and/or the Apms Server is still active.\nShut down anyway?"), MSGBOX_TITLE_V, MB_YESNO);
		if(res_i == IDYES){
			//DeactivateLog();
			exit(-1);
		}
	}
	else{
		//DeactivateLog();
		exit(1);
	}
}

/**********************************************************************************/
void CVisual_BuilderDlg::OnAcquire(){
	RETSTAT		retSt;
	int			res_i, zoneCount, totCells, micrNum, macrNum;
	FILEIDX		locFile;
	BOOL		tempZone, tempCell, tempObjt, wrn;
	ZONENAME	zoneName;
	int			locLevel;
	bool		keepObj;
	bool		chA;
	bool		chB;

	retSt = GetLoggedUserLevel(this->m_clientNum, &locLevel);
	if((retSt != RT_NORMAL) || (locLevel == -1)){
		MessageBox(_T("No Tenore user currently logged.\nPlease login and try again."), _T("Warning"), MB_OK | MB_ICONWARNING);
		return;
	}
	else if((locLevel == LEVEL3_SECGRO) || (locLevel == LEVEL4_SECGRO) || (locLevel == LEVEL5_SECGRO)){
		MessageBox(_T("Insufficent privileges.\nJust system manager, level 1 and 2 can import data"), _T("Warning"), MB_OK | MB_ICONWARNING);
		return;
	}

	if(CheckUser())
		return;

	locFile.num = F_ZONE;
	UpdateData(true);
	tempZone = m_CheckZone;
	tempCell = m_CheckCell;
	tempObjt = m_CheckObjt;
//	if(m_CheckLog == 0)
	//	DeactivateLog();
		
	//else{
		ActivateLog();
		TCHAR	     temp[_MAX_PATH];
		_tcsncpy_s(temp, m_PathLog,sizeof(m_PathLog));
		SetLogPath(temp);
	

	//}
	
	m_ProgBar.SetPos(0);
	UpdateData(true);
	
	
	if(!ApmsActive){
		_stprintf_s(msg, _T("Unable to acquire no Apms server Activated."));
		echo(true);
	}
	else if(!XlsActive){
		_stprintf_s(msg, _T("Unable to acquire no Excel document opened."));
		echo(true);
	}
	else{
		if(m_CheckZone){
			m_ProgBar.SetPos(0);
			zoneCount = 0;
			res_i = AcquireZone(excelObj, &zoneCount, this->m_hWnd);
			if(res_i == zoneCount){
				GetWrnFlag(&wrn);
				if(wrn){
					_stprintf_s(msg, _T("Warning detected, check log file."));
					echo(true);
				}
				_stprintf_s(msg, _T("%d zones successfully acquired."), zoneCount);
				echo(true);
				_stprintf_s(msg, _T("sp"));
				echo(true);
			}
			else if (res_i > 0){
				_stprintf_s(msg, _T("Warning zoneCount = %d and Acquire zone exited with %d. Check log file."), zoneCount, res_i);
				echo(true);
				_stprintf_s(msg, _T("sp"));
				echo(true);
			}
			else{
				_stprintf_s(msg, _T("Error acquirng zone data. Check log file."));
				echo(true);
				_stprintf_s(msg, _T("sp"));
				echo(true);
			}
		}
		if(m_CheckCell){
			res_i = F_ZONE_Check();
			if(res_i == 0){
				_stprintf_s(msg, _T("Error checking zone data. Check log file."));
				echo(true);
			}

			if(this->m_chkKeep.GetCheck() != 0)
				keepObj = true;
			else
				keepObj = false;
			
			if(this->m_cmbZones.GetCurSel() == 0){
				totCells = AcquireAllCells(excelObj, keepObj, this->m_hWnd);
				if(totCells != -1){
					_stprintf_s(msg, _T("Successfully acquired cells for %d zones"), totCells);
					echo(true);
					_stprintf_s(msg, _T("sp"));
					echo(true);
				}
				else{
					_stprintf_s(msg, _T("Error acquiring cells data. Check log file."));
					echo(true);
					_stprintf_s(msg, _T("sp"));
					echo(true);
				}
			}else{
				this->m_cmbZones.GetLBText(this->m_cmbZones.GetCurSel(), zoneName);
				totCells = AcquireCell(excelObj, zoneName, keepObj, this->m_hWnd);
				if(totCells == -1){
					_stprintf_s(msg, _T("Error acquiring zone %s cells data. Check log file."), zoneName);
					echo(true);
					_stprintf_s(msg, _T("sp"));
					echo(true);
				}else{
					_stprintf_s(msg, _T("Successfully acquired %d cells for zone %s."), totCells, zoneName);
					echo(true);
					_stprintf_s(msg, _T("sp"));
					echo(true);
				}
			
			}
		}
		if(m_CheckObjt){
			m_ProgBar.SetPos(0);
			res_i = F_ZONE_Check();
			if(res_i == 0){
				_stprintf_s(msg, _T("Error checking zone data. Check log file."));
				echo(true);
			}
/*
			if(res_i < N_MAX_ZONE){
				_stprintf_s(msg, "Zone data missing, load zone data first.");
				echo(true);
				_stprintf_s(msg, "sp");
				echo(true);
			}
			else{
*/
				res_i = AcquireObjT(excelObj, this->m_hWnd);
				if(res_i == 0){
					_stprintf_s(msg, _T("Object types data acquisition failed. Check log file."));
					echo(true);
				}
				else{
					GetWrnFlag(&wrn);
					if(wrn){
						_stprintf_s(msg, _T("Warning detected, check log file."));
						echo(true);
					}
					_stprintf_s(msg, _T("%d Object types loaded successfully."), res_i);
					echo(true);
				}
			}
//		}
		
		if(m_CheckPhas){
			m_ProgBar.SetPos(0);
			//res_i = AcquirePhas(excelObj, excelInst, EXCEL_SHEET_PHAS);
			res_i = AcquireTecc(excelObj, this->m_hWnd);
			if(res_i == 0){
				_stprintf_s(msg, _T("Technological cycles data acquisition failed check log file."));
				echo(true);
				_stprintf_s(msg, _T("sp"));
				echo(true);
			}
			else{
				_stprintf_s(msg, _T("%d Technological cycles acquired successfully."), res_i);
				echo(true);
				_stprintf_s(msg, _T("sp"));
				echo(true);
			}
		}

		/*
		if(m_CheckObje){
			//Acquisizione dati degli oggetti.
			m_ProgBar.SetPos(0);
			res_i = F_ZONE_Check();
			if(res_i == 0){
				_stprintf_s(msg, "Error checking zone data. Check log file.");
				echo(true);
			}

			if(res_i < N_MAX_ZONE){
				_stprintf_s(msg, "Zone data missing, load zone data first.");
				echo(true);
				_stprintf_s(msg, "sp");
				echo(true);
			}

			res_i = F_OBJT_Check();
			if(res_i == 0){
				_stprintf_s(msg, "Error checking object type data. Check log file.");
				echo(true);
			}
			
			if(res_i == 0){
				_stprintf_s(msg, "Object type data missing, load object types data first.");
				echo(true);
				_stprintf_s(msg, "sp");
				echo(true);
			}

			res_i = AcquireObj(excelObj, &objNum, this->m_hWnd);
			if(res_i == 0){
				_stprintf_s(msg, "Object type data acquisition failed check log file.");
				echo(true);
				_stprintf_s(msg, "sp");
				echo(true);
			}
			else{
				GetWrnFlag(&wrn);
				if(wrn){
					_stprintf_s(msg, "Warning detected, check log file.");
					echo(true);
				}
				_stprintf_s(msg, "%d objects acquired successfully.", objNum);
				echo(true);
				_stprintf_s(msg, "sp");
				echo(true);
			}
		}
		*/

		if(m_CheckTool){
			//Acquisizione dati dei tool.
			m_ProgBar.SetPos(0);
			res_i = F_ZONE_Check();
			if(res_i == 0){
				_stprintf_s(msg, _T("Error checking zone data. Check log file."));
				echo(true);
			}

			if(res_i < N_MAX_ZONE){
				_stprintf_s(msg, _T("Zone data missing, load zone data first."));
				echo(true);
				_stprintf_s(msg, _T("sp"));
				echo(true);
			}
		}

		if(m_CheckMicro){
			//Acquisizione dati delle micro operazioni.
			m_ProgBar.SetPos(0);
			res_i = AcquireMicr(excelObj, &micrNum, this->m_hWnd);
			if(res_i == -1){
				_stprintf_s(msg, _T("Error retrieving Micro operations data. Check log file."));
				echo(true);
			}
			_stprintf_s(msg, _T("%d Micro operations data successfully acquired."), micrNum);
			echo(true);
		}

		if(m_CheckMacro){
			//Acquisizione dati delle macro operazioni.
			m_ProgBar.SetPos(0);
			res_i = AcquireMacr(excelObj, &macrNum, this->m_hWnd);
			if(res_i == -1){
				_stprintf_s(msg, _T("Error retrieving Macro operations data. Check log file."));
				echo(true);
			}
			else{
				GetWrnFlag(&wrn);
				if(wrn){
					_stprintf_s(msg, _T("Warning detected, check log file."));
					echo(true);
				}
				_stprintf_s(msg, _T("%d Macro operations data successfully acquired."), macrNum);
				echo(true);
			}
		}

		if(m_CheckOseq){
			//Acquisizione dati delle sequenze operative
			m_ProgBar.SetPos(0);
			res_i = F_ZONE_Check();
			if(res_i == 0){
				_stprintf_s(msg, _T("Error checking zone data. Check log file."));
				echo(true);
			}
/*
			if(res_i < N_MAX_ZONE){
				_stprintf_s(msg, "Zone data missing, load zone data first.");
				echo(true);
				_stprintf_s(msg, "sp");
				echo(true);
			}
*/
			res_i = AcquireOseq(excelObj, this->m_hWnd);
			if(res_i == -1){
				_stprintf_s(msg, _T("Error retrieving Operational sequences' data. Check log file."));
				echo(true);
			}
			else{
				GetWrnFlag(&wrn);
				if(wrn){
					_stprintf_s(msg, _T("Warning detected, check log file."));
					echo(true);
				}
				_stprintf_s(msg, _T("%d Operational sequences data successfully acquired."), res_i);
				echo(true);
			}
		}

		if(m_StartCond){
			res_i = AcquireTCStartCond(excelObj, this->m_hWnd);
			if(res_i <= 0){
				_stprintf_s(msg, _T("Error acquiring Tech Cycle start conditions."));
				echo(true);
			}
			else{
				_stprintf_s(msg, _T("%d Tech Cycle start conditions successfully acquired."), res_i);
				echo(true);
			}
		}

		if(m_TcSeq){
			res_i = AcquireTCSeq(excelObj, this->m_hWnd);
			if(res_i <= 0){
				_stprintf_s(msg, _T("Error acquiring Tech Cycle sequences."));
				echo(true);
			}
			else{
				_stprintf_s(msg, _T("%d Tech Cycle sequences successfully acquired."), res_i);
				echo(true);
			}
		}

		if(this->m_ChkInfi){
			if(this->m_chkChA.GetCheck() != 0)
				chA = true;
			else
				chA = false;
			if(this->m_chkChB.GetCheck() != 0)
				chB = true;
			else
				chB = false;
			res_i = AcquireInfi90Data(excelObj, this->m_clientNum, this->m_hWnd, chA,chB);
			if(res_i <= 0){
				_stprintf_s(msg, _T("Error acquiring Infi90 Tune data."));
				echo(true);
			}
			else{
				_stprintf_s(msg, _T("%d Infi90 Tune data successfully acquired."), res_i);
				echo(true);
			}
		}
		
		if(this->m_ChkMntn){
			res_i = AcquireMaintData(excelObj, excelInst, this->m_clientNum, this->m_hWnd);
			if(res_i <= 0){
				_stprintf_s(msg, _T("Error acquiring Maintenance data."));
				echo(true);
			}
			else{
				_stprintf_s(msg, _T("%d Maintenance entry data successfully acquired."), res_i);
				echo(true);
			}
		}

		if(this->m_TcErrCodes){
			res_i = AcquireTCErr(excelObj, excelInst, this->m_clientNum, this->m_hWnd);
			if(res_i <= 0){
				_stprintf_s(msg, _T("Error acquiring Technological cycles error codes."));
				echo(true);
			}
			else{
				_stprintf_s(msg, _T("%d Technological cycles error codes successfully acquired."), res_i);
				echo(true);
			}
		
		}

		if(!m_ChkInfi && !m_CheckZone && !m_CheckCell && !m_CheckObjt && !m_ChkMntn && !m_CheckTool && !m_CheckOseq && !m_CheckMicro && !m_CheckMacro && !m_CheckPhas && !m_StartCond && ! m_TcSeq && !this->m_TcErrCodes){
			_stprintf_s(msg, _T("No acquisition option choosen."));
			echo(true);
		}
	}
}

/**********************************************************************************/	

void CVisual_BuilderDlg::OnOpenLog() 
{
	CFileDialog	fileTxt(TRUE, _T("txt"),_T(""), NULL,_T("Text files|*.txt|All files|*.*||"));
	if(fileTxt.DoModal() == IDOK){
		m_PathLog.Empty();
		m_PathLog = fileTxt.GetPathName();
		UpdateData(false);
	}	
}

/**********************************************************************************/	


void CVisual_BuilderDlg::OnFileview() 
{
	if(CheckUser())
		return;

	CFileView *tempViewer;
	if (ApmsActive){
		tempViewer = new CFileView;
		tempViewer->Create(IDD_FILEVIEW, NULL);
		tempViewer->ShowWindow(SW_SHOW);
	}
	else{
		_stprintf_s(msg, _T("Connect to Apms server first."));
		echo(true);
	}
}

bool CVisual_BuilderDlg::ActivateControls(){
	RETSTAT		retSt;

	retSt = GetLoggedUserLevel(this->m_clientNum, &this->m_loggedUser);
	if(retSt != RT_NORMAL)
		return(false);

	switch(this->m_loggedUser){
	case SYSMGR:
	case LEVEL1:
	case LEVEL2:
		this->m_Acquire.EnableWindow(true);
		this->m_OpenLog.EnableWindow(true);
		this->m_FileViewButton.EnableWindow(true);
		this->m_ConnectButton.EnableWindow(true);
		//this->m_OpenXlsButton.EnableWindow(true);
		break;
	case LEVEL3:
	case LEVEL4:
		this->m_Acquire.EnableWindow(false);
		this->m_OpenLog.EnableWindow(false);
		this->m_FileViewButton.EnableWindow(true);
		this->m_ConnectButton.EnableWindow(true);
		//this->m_OpenXlsButton.EnableWindow(false);
		break;
	case LEVEL5:
		this->m_Acquire.EnableWindow(false);
		this->m_OpenLog.EnableWindow(false);
		this->m_FileViewButton.EnableWindow(false);
		this->m_ConnectButton.EnableWindow(false);
		//this->m_OpenXlsButton.EnableWindow(false);
		break;
	}

	return(true);
}

bool CVisual_BuilderDlg::CheckUser(){
	RETSTAT		retSt;
	int			currentUser;
	
	currentUser = this->m_loggedUser;
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(this->m_clientNum, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."), _T("Error"), MB_OK);
		return false;
	}
	if(m_loggedUser != currentUser){
		ActivateControls();
		return true;
	}
	return false;
}

LRESULT CVisual_BuilderDlg::OnTntBuild(WPARAM wParam, LPARAM lParam){
	switch(wParam){
	case TNT_BUILD_START:
		this->m_ProgBar.SetRange32(0, lParam);
		this->m_ProgBar.SetPos(0);
		this->m_ProgBar.SetStep(1);
		break;
	case TNT_BUILD_STEP:
		this->m_ProgBar.StepIt();
		break;
	case TNT_BUILD_END:
		
		break;
	default:
		break;
	}
	return 0;
}

BOOL CVisual_BuilderDlg::fillZones(void){
	RETCOND		retCnd;
	FILEIDX		fileIdx;
	t_zone		zone;
	int			i;
	int			pos;

	fileIdx.num = F_ZONE;

	this->m_cmbZones.ResetContent();

	this->m_cmbZones.InsertString(0, _T("ALL ZONES"));
	this->m_cmbZones.SetItemData(0, 0);

	for(i = 1; i <= N_MAX_ZONE; i++){
		retCnd = FileRead(&fileIdx, i, 1, &zone, sizeof(t_zone));
		if(retCnd != R_NORMAL)
			break;
		
		if((_tcslen(zone.name) > 0) && (zone.virtualZone == 0)){
			pos = this->m_cmbZones.AddString(zone.name);
			this->m_cmbZones.SetItemData(pos, i);
		}
	}

	this->m_cmbZones.SetCurSel(0);

	this->m_cmbZones.EnableWindow(true);

	this->UpdateData(false);

	return(TRUE);
}


void CVisual_BuilderDlg::OnClose(){
	

	int		res_i;
	BOOL	res_b;
	
	_stprintf_s(msg, _T("Exiting..."));
	echo(true);
	
	if(ApmsActive){
		res_i = DeactivateApmsServer();
		if(res_i){
			_stprintf_s(msg, _T("Disonnection to %s Apms server failed."), m_Server);
			echo(true);
		}
		else{
			_stprintf_s(msg, _T("Apms server %s disconnected"), m_Server);
			echo(true);
			ApmsActive = false;
		}
	}

	if(XlsActive){
		res_b = excelObj->Close();
		if(res_b == false){
			_stprintf_s(msg, _T("Error closing Excel document."));
			echo(true);
		}
		else{
			_stprintf_s(msg, _T("Excel document closed."));
			echo(true);
			XlsActive = false;
		}
	}
	
	if(XlsActive || ApmsActive){
		res_i = MessageBox(_T("Either an Excel file is still opened and/or the Apms Server is still active.\nShut down anyway?"), MSGBOX_TITLE_V, MB_YESNO);
		if(res_i == IDYES){
			CDialog::OnClose();
		}
	}else
		CDialog::OnClose();
	
	
}
